package pers.qianyu.month_202012.date_20201226;

import java.util.*;
import java.util.stream.*;

/**
 * 1042. 不邻接植花
 * https://leetcode-cn.com/problems/flower-planting-with-no-adjacent/
 *
 * @author mizzle rain
 * @date 2020-12-26 20:42
 */
public class GardenNoAdj {
    public int[] gardenNoAdj(int n, int[][] paths) {
        HashMap<Integer, Set<Integer>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            HashSet<Integer> set = new HashSet<>();
            map.put(i + 1, set);
        }
        // 构建邻接链表
        for (int[] path : paths) {
            map.get(path[0]).add(path[1]);
            map.get(path[1]).add(path[0]);
        }
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            List<Integer> list = Stream.of(1, 2, 3, 4).collect(Collectors.toList());
            Set<Integer> set = map.get(i + 1);
            for (int j : set) {
                if (ans[j - 1] != 0) {
                    list.remove(Integer.valueOf(ans[j - 1]));
                }
            }
            if (ans[i] == 0) {
                ans[i] = list.remove(0);
            }
        }
        return ans;
    }
}
